<template>
  <div class="lyb-box" ref="u-dom">
    <header class="lyb-box-header" ref="u-dom-drag">
      <ins class="lyb-box-header-end">留言板</ins>
      <ins
        class="lyb-box-header-start"
        :id="window_ ? '' : 'lyb-box-header-start'"
        @touchstart="window_Handler($event, false)"
        @click="window_Handler($event, true)"
      ></ins>
    </header>
    <div class="lyb-box-sx" v-show="window_">
      <ins class="nb_nodeboard_text">
        <p class="nb_nodeboard_text-p">
          感谢您的关注，当前客服人员不在线，请填写一下您的信息，我们会尽快和您联系。
        </p>
      </ins>
      <div class="nb_nodeboard_set" v-if="AqyzZos">
        <ins v-for="i in data" :key="i.id">
          <div
            v-if="i.type"
            :class="
              'nb-nodeboard-ipt ' +
              i.className +
              (i.errorFlag ? ' nb-nodeboard-param' : ' ')
            "
          >
            <ins></ins>
            <input type="text" :placeholder="i.title" v-model="i.val" />
          </div>
          <ins class="nb-nodeboard-name" v-else-if="!i.type">
            <textarea
              :placeholder="i.title"
              :class="i.errorFlag ? 'nb-nodeboard-param' : ''"
              v-model="i.val"
            ></textarea>
          </ins>
          <ins class="nb-nodeboard-tips" v-if="i.error && i.errorFlag">{{
            i.errorTitle
          }}</ins>
        </ins>
        <ins class="nb-nodeboard-send-btn-box">
          <ins class="nb-nodeboard-send-btn" @click="Submit">提交</ins>
        </ins>
      </div>
      <div v-else class="nb-nodeboard-success">
        <ins class="nb-nodeboard-success_ins">
          <ins class="nb-nodeboard-success_ins_img">
            <img src="/img/bridge.png" />
          </ins>
          <ins class="nb-nodeboard-success_title">
            <span>提交失败</span>
            <p>提交失败请刷新重试</p>
          </ins>
          <ins class="nb-nodeboard-success_clons">
            <button v-on:click="AqyzZosFun(true)">返回</button>
          </ins>
        </ins>
      </div>
    </div>
  </div>
</template>
<script setup>
import { reactive, getCurrentInstance, onMounted, ref } from "vue";
import { useUserStore } from "../../pinia/store.js";
import { storeToRefs } from "pinia";
const user_store = useUserStore();
const { AqyzZos  } = storeToRefs(user_store);
const { AqyzZosFun } = user_store;
const {
  attrs: { aqyzFun },
} = getCurrentInstance();
const window_ = ref(true);
const window_Handler = function (e, v) {
  e.stopPropagation();
  if (IsPC() !== v) return;
  window_.value = !window_.value;
};
// 判读当前是什么pc/移动
function IsPC() {
  var userAgentInfo = navigator.userAgent;
  var Agents = new Array(
    "Android",
    "iPhone",
    "SymbianOS",
    "Windows Phone",
    "iPad",
    "iPod"
  );
  var flag = true;
  for (var v = 0; v < Agents.length; v++) {
    if (userAgentInfo.indexOf(Agents[v]) > 0) {
      flag = false;
      break;
    }
  }
  return flag;
}
onMounted(function () {
  const { refs } = getCurrentInstance();
  const uDom = refs["u-dom"];
  const uDomDrag = refs["u-dom-drag"];
  const pageMove = {
    x: 0,
    y: 0,
  };
  const move = function (e) {
    const ev = e.changedTouches?.[0] || undefined;
    const { clientX, clientY } = ev || e;
    uDom.style.left = clientX - pageMove.x + "px";
    uDom.style.top = clientY - pageMove.y + "px";
  };
  const down = function (e) {
    e?.preventDefault?.();
    e?.stopPropagation?.();
    const ev = e.changedTouches?.[0] || undefined;
    const { clientX, clientY } = ev || e;
    pageMove.x = clientX - uDom.offsetLeft;
    pageMove.y = clientY - uDom.offsetTop;
    document.addEventListener("touchmove", move);
    document.addEventListener("mousemove", move);
  };
  const up = function () {
    pageMove.x = 0;
    pageMove.y = 0;
    document.removeEventListener("touchmove", move);
    document.removeEventListener("mousemove", move);
    if (uDom.offsetLeft <= 0) {
      uDom.style.left = 0 + "px";
    } else if (uDom.offsetLeft >= document.documentElement.clientWidth) {
      uDom.style.left =
        document.documentElement.clientWidth - uDom.offsetWidth + "px";
    }
    if (uDom.offsetTop <= 0) {
      uDom.style.top = 0 + "px";
    } else if (uDom.offsetTop >= document.documentElement.clientHeight) {
      uDom.style.top =
        document.documentElement.clientHeight - uDom.offsetHeight + "px";
    }
  };
  uDomDrag.ontouchstart = down;
  uDomDrag.onmousedown = down;
  uDomDrag.ontouchend = up;
  uDomDrag.onmouseup = up;
});
const data = reactive([
  {
    title: "请在此输入留言内容，我们会尽快与您联系。（必填）",
    className: "nb-nodeboard-name",
    id: 0,
    error: true,
    type: 0,
    errorTitle: "留言内容不能为空",
    errorFlag: false,
    val: "123123",
  },
  {
    title: "名字",
    className: "nb-nodeboard-ipt-name",
    id: 1,
    error: false,
    type: 1,
    errorFlag: false,
    val: "1231231",
  },
  {
    title: "电话",
    className: "nb-nodeboard-ipt-dh",
    id: 2,
    error: true,
    errorTitle: "电话不能为空",
    type: 1,
    errorFlag: false,
    val: "17639985251",
  },
  {
    title: "身高 (必填)",
    className: "nb-nodeboard-ipt-sg",
    id: 3,
    error: true,
    errorTitle: "身高不能为空",
    type: 1,
    errorFlag: false,
    val: "1231",
  },
  {
    title: "体重",
    className: "nb-nodeboard-ipt-tz",
    id: 4,
    error: false,
    type: 1,
    errorFlag: false,
    val: "",
  },
]);
// 控制安全验证的值
const verificationDone = ref(false);
// 安全验证
const verification = function () {
  aqyzFun(true);
};
const reg =
          /^(13[0-9]|14[01456879]|15[0-3,5-9]|16[2567]|17[0-8]|18[0-9]|19[0-3,5-9])\d{8}$/;
// 提交事件
const Submit = function (e) {
  e.preventDefault();
  e.stopPropagation();
  for (let i = 0; i < data.length; i++) {
    data[i].errorFlag = false;
    if (data[i].id === 0 || data[i].id === 2 || data[i].id === 3) {
      const { val } = data[i];
      var done = false;
      if (data[i].id === 0) {
        for (let q of val) if (q !== " ") done = true;
      } else if (data[i].id === 2) {
        const reg =
          /^(13[0-9]|14[01456879]|15[0-3,5-9]|16[2567]|17[0-8]|18[0-9]|19[0-3,5-9])\d{8}$/;
        if (reg.test(val)) {
          done = true;
          data[i].errorTitle = "电话不能为空";
        } else if (val) {
          done = false;
          data[i].errorTitle = "请正确输入号码";
        }
      } else if (data[i].id === 3 && val) {
        console.log(1);
        done = true;
      }
      if (done && i >= 3) {
        // 调用方法  给他一个懒加载效果 运用了演示器
        setTimeout(function () {
          verification();
        }, 1000);
      } 
      if(!done) {
        data[i].errorFlag = true;
        break;
      }
    }
  }
};
</script>
<style scoped>
.nb-nodeboard-success {
  width: 100%;
  height: auto;
  background-color: #f7f7f7f7;
  padding-top: 2rem;
  box-sizing: border-box;
}
.nb-nodeboard-success_ins {
  width: 6rem;
  height: auto;
  margin: 0 auto;
}
.nb-nodeboard-success_ins_img {
  width: 2rem;
  height: 2rem;
  margin: 0 auto 0.4rem;
}
.nb-nodeboard-success_ins_img img {
  width: 100%;
  height: 100%;
}
.nb-nodeboard-success_title {
  width: 100%;
  height: auto;
}
.nb-nodeboard-success_title span {
  display: block;
  text-align: center;
  font-size: 0.5rem;
  color: #000;
  margin: 0.2rem 0;
}
.nb-nodeboard-success_title p {
  text-align: center;
  font-size: 0.4rem;
  color: #666;
}
.nb-nodeboard-success_clons {
  width: 100%;
  height: 3rem;
  margin-top: 1rem;
}
.nb-nodeboard-success_clons button {
  display: block;
  width: 4rem;
  height: 1.4rem;
  border: none;
  outline: none;
  color: #fff;
  background: #6696ff;
  font-size: 0.5rem;
  margin: 0 auto;
}
.lyb-box {
  width: 9.5rem;
  height: auto;
  background-color: #fff;
  position: fixed;
  left: 2rem;
  top: 2rem;
  border-radius: 0.25rem;
  overflow: hidden;
  z-index: 10000;
}

.lyb-box-header {
  width: 100%;
  height: 2rem;
  background-color: #6696ff;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.lyb-box-header ins {
  padding-left: 0.4rem;
  padding-right: 0.4rem;
}

.lyb-box-header-start {
  width: 2rem;
  height: 100%;
  background: url("/img/nodeMin.png") no-repeat center right;
  margin-right: 0.4rem;
  background-size: 0.8rem 0.5rem;
}
#lyb-box-header-start {
  background: url("/img/nodeMax.png") no-repeat center right !important;
  background-size: 1.4rem 1.4rem !important;
}
.lyb-box-header-end {
  color: #fff;
  font-size: 0.5rem;
  text-decoration: none;
  font-weight: 600;
}

.lyb-box-sx {
  width: 100%;
  height: auto;
  padding: 0 0.4rem;
  box-sizing: border-box;
}

.nb_nodeboard_text p {
  font-size: 0.4rem;
  background-color: transparent;
  color: #303133;
  word-break: break-all;
  line-height: 0.8rem;
  margin-top: 0.2rem;
  margin-bottom: 0.6rem;
}

.nb-nodeboard-name {
  width: 100%;
  position: relative;
}

.nb-nodeboard-name textarea {
  width: 100%;
  height: 3.625rem;
  padding: 0.2rem 0.5rem;
  resize: none;
  overflow: hidden;
  border: 0.0625rem solid #dcdfe6;
  background-color: #fff;
  text-align: left;
  outline: 0;
  border-radius: 0.25rem;
  font-size: 0.4rem;
  line-height: 0.8rem;
  box-sizing: border-box;
  font-family: Inter, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
    Oxygen, Ubuntu, Cantarell, "Fira Sans", "Droid Sans", "Helvetica Neue",
    sans-serif;
}

.nb-nodeboard-name textarea::placeholder {
  color: #ccc;
}

.nb-nodeboard-tips {
  color: #e64552;
  font-size: 0.4rem;
}

.nb-nodeboard-param {
  border-color: #e64552 !important;
}

.nb-nodeboard-ipt {
  width: 100%;
  height: 1.2rem;
  border: 1px solid #dcdfe6;
  font-weight: 0.6rem;
  margin-top: 0.4rem;
}

.nb-nodeboard-ipt ins {
  width: 1.5rem;
  height: 100%;
  display: inline-block;
  text-align: center;
  background-color: #fff;
  vertical-align: top;
  border-right: 1px solid #fff;
  border-radius: 4px 0 0 4px;
}

.nb-nodeboard-ipt input {
  width: 7rem;
  height: 100%;
  line-height: 1.2rem;
  border: none;
  display: inline-block;
  outline: 0;
  padding-left: 0.2rem;
  font-size: 0.2rem;
}

.nb-nodeboard-ipt-name ins {
  background: #fff url("/img/user.png") no-repeat 0.5625rem center;
}

.nb-nodeboard-ipt-dh ins {
  background: #fff url("/img/nodephone.png") no-repeat 0.5625rem center;
}

.nb-nodeboard-ipt-sg ins {
  background: #fff url("/img/custom-select.png") no-repeat 0.5625rem center;
}

.nb-nodeboard-ipt-tz ins {
  background: #fff url("/img/custom-select.png") no-repeat 0.5625rem center;
}
.nb-nodeboard-send-btn-box {
  width: 100%;
  height: 1.2rem;
  margin-top: 0.4rem;
  margin-bottom: 0.4rem;
}
.nb-nodeboard-send-btn {
  height: 1.2rem;
  width: 2.2rem;
  text-align: center;
  border-radius: 0.1rem;
  font-size: 0.4rem;
  line-height: 1.2rem;
  color: #fff;
  cursor: pointer;
  float: right;
  background-color: #6696ff;
}
</style>
